package com.sdcx.agrobank.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sdcx.agrobank.R;
import com.sdcx.agrobank.adapter.FragmentAdapter;
import com.sdcx.agrobank.ui.fragment.lcFragment;
import com.sdcx.agrobank.ui.fragment.mainFragment;
import com.sdcx.agrobank.ui.fragment.shFragment;
import com.sdcx.agrobank.ui.fragment.wdFragment;
import com.sdcx.agrobank.ui.fragment.xykFragment;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity {

    private RadioButton sy, lc, xyk, sh, wd;
    private ViewPager vp;
    private RadioGroup viewById;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        sy = findViewById(R.id.sy);
        lc = findViewById(R.id.lc);
        xyk = findViewById(R.id.xyk);
        sh = findViewById(R.id.sh);
        wd = findViewById(R.id.wd);
        vp = findViewById(R.id.vp);
        List<Fragment> arrayList = new ArrayList<>();
        arrayList.add(new mainFragment());
        arrayList.add(new lcFragment());
        arrayList.add(new xykFragment());
        arrayList.add(new shFragment());
        arrayList.add(new wdFragment());
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), arrayList);
        //设置适配器
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(4);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==0) {
                    sy.setChecked(true);
                    lc.setChecked(false);
                    xyk.setChecked(false);
                    sh.setChecked(false);
                    wd.setChecked(false);
                } else if (position==1) {
                    sy.setChecked(false);
                    lc.setChecked(true);
                    xyk.setChecked(false);
                    sh.setChecked(false);
                    wd.setChecked(false);
                }else if (position==2) {
                    sy.setChecked(false);
                    lc.setChecked(false);
                    xyk.setChecked(true);
                    sh.setChecked(false);
                    wd.setChecked(false);
                }else if (position==3) {
                    sy.setChecked(false);
                    lc.setChecked(false);
                    xyk.setChecked(false);
                    sh.setChecked(true);
                    wd.setChecked(false);
                }else if (position==4) {
                    sy.setChecked(false);
                    lc.setChecked(false);
                    xyk.setChecked(false);
                    sh.setChecked(false);
                    wd.setChecked(true);
                }
                getButtonJudge();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewById = findViewById(R.id.radio_group);
        viewById.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.sy:
                        getButtonJudge();
                        vp.setCurrentItem(0);
                        break;
                    case R.id.lc:
                        getButtonJudge();
                        vp.setCurrentItem(1);
                        break;
                    case R.id.xyk:
                        getButtonJudge();
                        vp.setCurrentItem(2);
                        break;
                    case R.id.sh:
                        getButtonJudge();
                        vp.setCurrentItem(3);
                        break;
                    case R.id.wd:
                        getButtonJudge();
                        vp.setCurrentItem(4);
                        break;
                }
            }
        });

    }

    private void getButtonJudge() {
        if (sy.isChecked()) {
            sy.setTextColor(Color.argb(255, 24, 205, 192));
        } else {
            sy.setTextColor(Color.argb(255, 43, 43, 43));
        }
        if (lc.isChecked()) {
            lc.setTextColor(Color.argb(255, 24, 205, 192));
        } else {
            lc.setTextColor(Color.argb(255, 43, 43, 43));
        }
        if (xyk.isChecked()) {
            xyk.setTextColor(Color.argb(255, 24, 205, 192));
        } else {
            xyk.setTextColor(Color.argb(255, 43, 43, 43));
        }
        if (sh.isChecked()) {
            sh.setTextColor(Color.argb(255, 24, 205, 192));
        } else {
            sh.setTextColor(Color.argb(255, 43, 43, 43));
        }
        if (wd.isChecked()) {
            wd.setTextColor(Color.argb(255, 24, 205, 192));
        } else {
            wd.setTextColor(Color.argb(255, 43, 43, 43));
        }
    }
}